export default{
	namespaced:true,
	
	state:() =>({
		cart:JSON.parse(uni.getStorageSync('cart')||'[]')//存放项目的信息对象属性
	}),
	mutations:{
		addTocart(state,goods){
			const findResult=state.cart.find((x)=>x.goods_id===goods.goods_id)
			console.log(findResult)
			if(!findResult){
				state.cart.push(goods)
			}else{
				findResult.goods_count++
			}
			this.commit('m-cart/saveToStorage')
			console.log(state.cart)
		},
		saveToStorage(state){
			uni.setStorageSync('cart',JSON.stringify(state.cart))
		},
		removeGoods(state,goods_id){
			state.cart=state.cart.filter(x=>x.goods_id!==goods_id)
			this.commit('m-cart/saveToStorage')
		}
	},
	getters:{
		total(state){
			// let c=0
			// state.cart.forEach(x=>c+=x.goods_count)
			// return c
			return state.cart.reduce((total,item)=>total+=item.goods_count,0)
		},
		//已勾选商品总数量
		checked(state){
			return state.cart.filter(x=>x.goods_selected).reduce((total,item)=>total+=item.goods_count,0)
		},
		CheckedAmout(state){
			return state.cart.filter(x=>x.goods_selected).reduce((total,item)=>total+=item.goods_count*item.goods_price,0).toFixed(2)
		}
	}
}